/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.iubar.tkdscore;

import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.util.Calendar;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author borgo
 */
public class Clock2 extends Clock {

    private long start_time = 0;
    private double ss = 0;
    private double mm = 0;
    private double hh = 0;
    private double dd = 0;
    private boolean paused = false;
    private long paused_time = 0;
    private double ss_delta = 0;
    private double mm_delta = 0;
    private double hh_delta = 0;
    private int status = 0;
    private static final int STARTED = 1;
    private static final int PAUSED = 2;
    private static final int STOPPED = 3;

    private static int round_mm_as_int = 0;
    private static int round_ss_as_int = 0;

    /** Start the timer. */
    public void start() {
        if (!paused) {
            reset();
        }
        this.start_time = System.currentTimeMillis();
        this._timer.start();
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        toolkit.beep();
        System.out.println("Beep!");
        this.status = STARTED;



 
        String str1 = TkdScoreApp.view.getRoundMm();
        String str2 = TkdScoreApp.view.getRoundSs();
        round_mm_as_int = TkdGlobal.toInt(str1);
        round_ss_as_int = TkdGlobal.toInt(str2);
    }

    protected void updateTime() {
        //... Avoid creating new objects.
        long delta = System.currentTimeMillis() - start_time;

// Get elapsed time in milliseconds
        long elapsedTimeMillis = System.currentTimeMillis() - start_time;

// Get elapsed time in seconds
        ss = elapsedTimeMillis / 1000F;
        ss = ss + ss_delta;
        ss = ss % 60;

// Get elapsed time in minutes
        mm = elapsedTimeMillis / (60 * 1000F);
        mm = mm + mm_delta;
        mm = mm % 60;

// Get elapsed time in hours
        hh = elapsedTimeMillis / (60 * 60 * 1000F);
        hh = hh + hh_delta;
        hh = hh % 24;


// Get elapsed time in days
// float elapsedTimeDay = elapsedTimeMillis/(24*60*60*1000F);


        int hh_as_int = (int) hh;
        int mm_as_int = (int) mm;
        int ss_as_int = (int) ss;
        String str = "" + format(hh_as_int) + ":" + format(mm_as_int) + ":" + format(ss_as_int);
        TkdScoreApp.view.getTimeLabel1().setText(str);
        TkdScoreApp.view.getTimeLabel2().setText(str);

        TkdScoreApp.view.getFieldMm().setText(String.valueOf(mm_as_int));
        TkdScoreApp.view.getFieldSs().setText(String.valueOf(ss_as_int));

        if((mm_as_int>=round_mm_as_int) && (ss_as_int>=round_ss_as_int)){
           TkdScoreApp.view.stopMatch();
        }

    }

    void reset() {
        this.ss = 0;
        this.mm = 0;
        this.hh = 0;
        this.dd = 0;
        this.ss_delta = 0;
        this.mm_delta = 0;
        this.hh_delta = 0;
        this.paused_time = 0;
        this.paused = false;
        repaint();
    }

    protected void drawClockHands(Graphics2D g2) {

        //... second hand
        int handMin = _diameter / 8;    // Second hand doesn't start in middle.
        int handMax = _diameter / 2;    // Second hand extends to outer rim.
        double fseconds = (ss + (double) mm / 1000) / 60.0;
        drawRadius(g2, fseconds, 0, handMax);

        //... minute hand
        handMin = 0;                    // Minute hand starts in middle.
        handMax = _diameter / 3;
        double fminutes = (mm + fseconds) / 60.0;
        drawRadius(g2, fminutes, 0, handMax);

        //... hour hand
        handMin = 0;
        handMax = _diameter / 4;
        drawRadius(g2, (hh + fminutes) / 12.0, 0, handMax);
    }

    public void pause() {
        _timer.stop();
        this.paused_time = System.currentTimeMillis();
        this.paused = true;
        this.status = PAUSED;
        this.ss_delta = this.ss;
        this.mm_delta = this.mm;
        this.hh_delta = this.hh;
    }

    public void setTime(double ss, double mm, double hh) {
        this.ss = ss;
        this.mm = mm;
        this.hh = hh;
        this.ss_delta = this.ss;
        this.mm_delta = this.mm;
        this.hh_delta = this.hh;
    }

    @Override
    public void stop() {
        super.stop();
        this.paused = false;
        this.status = STOPPED;
    }

    public int getStatus() {
        return this.status;
    }

    private String format(int a) {
        String str = "" + a;
        if (a < 10) {
            str = "0" + a;
        }
        return str;
    }
	
}
